<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>登陆页面</title>
  <link rel="stylesheet" href="../back/css/font-awesome.css">
</head>
<body>
<div id="bigBox">
  <h1>LOGIN</h1>
  <div class="inputBox">
    <form action="${pageContext.request.contextPath}/main/login" method="post">
      <div class="inputText">
        <i class="fa fa-user-circle" style="color: whitesmoke;"></i>
        <input type="text" placeholder="用户名" name="username" c/>
        <%System.out.println("asdadas");%>
      </div>
      <div class="inputText">
        <i class="fa fa-key" style="color: whitesmoke;"></i>
        <input type="password" placeholder="密码" name="password"/>
      </div>
      <input type="submit" class="inputButton" value="LOGIN" onclick="return submitForm(event);" />
    </form>
  </div>
</div>
</body>
<script>
  function submitForm(event) {
    event.preventDefault(); // 阻止表单默认提交行为

    var form = event.target.form;
    var formData = new FormData(form);

    fetch(form.action, {
      method: 'POST',
      body: formData
    })
            .then(data => {
              // 检查后端返回的code字段
              if (data.code == 3) {
                // 登录成功，存储并使用token
               // localStorage.setItem('authToken', data.token);

                // 登录成功逻辑
              //  console.log("登录成功222");
                window.location.href = "/techshine_war/back/index.html"; // 跳转至主页
                alert("登录成功");
              }
              else {
                // 登录失败逻辑
                console.error( '未知错误');
                // 在此处可以展示错误消息给用户，例如弹窗提示等
                alert(data.msg);
              }
            })

    return false;
  }
</script>
</html>
